package czk.concrent.enjoyModelWithNotLock.atomicRefenceFieldUpdater.practice;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/7/7 16:14
 */
public class AtomicFieldUpdaterPractice {
    public static void main(String[] args) {
        AtomicReferenceFieldUpdater<Person, String> fieldUpdater = AtomicReferenceFieldUpdater.newUpdater(Person.class, String.class, "name");
        Person person=new Person("杜兰特");
        boolean b = fieldUpdater.compareAndSet(person, "杜兰特", "詹姆斯");
        if (b) {
            System.out.println(person);
        }else {
            System.out.println("更新失败");
        }
    }
}
class Person{
    volatile String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}
